package 死锁;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class DeadLock {
    public static final String LOCKA = "A";
    public static final String LOCKB = "B";

    public static void main(String[] args) {
        //method01();
        new Thread("A线程") {
            @Override
            public void run() {
                synchronized (LOCKA) {
                    while (true) {
                        System.out.println(getName() + "获得LOCKA等待LOCKB！");
                        synchronized (LOCKB) {
                            System.out.println(getName() + "获得LOCKB成功进去小黑屋！");
                        }
                    }
                }
            }
        }.start();


        new Thread("B线程") {
            @Override
            public void run() {
                while (true) {
                    synchronized (LOCKB) {
                        System.out.println(getName() + "获得LOCKB等待LOCKA！");
                        synchronized (LOCKA) {
                            System.out.println(getName() + "获得LOCKA成功进去小黑屋！");
                        }
                    }
                }
            }
        }.start();
    }

    public static void method01() {
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("1");
                }
            }
        }.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("2");
                }
            }
        }).start();
    }


}
